package diona.snippet

import net.liftweb.util.CssSel
import net.liftweb.util.Helpers._
import scala.collection.JavaConversions._
import com.google.gdata.data.photos._
import diona.auth.GoogleOperations
import diona.model.{DropboxAuth, User, GoogleAuth}
import java.util.List

/**
 * User: anton
 * Date: 09.04.11 20:33
 */
class FilesList
{
  def files =
  {
    val user = User.currentUser.get
    val currentToken: Option[GoogleAuth] = GoogleAuth.byUser(user)
    currentToken match {
      case None => GoogleOperations.requestAuthorization("/network/files")
      case Some(token) => {
        val dbTokenOption: Option[DropboxAuth] = DropboxAuth.byUser(user)
        dbTokenOption match {
          case None => displayFiles(user) //DropboxAuthorizer.requestAuthorization("/network/files")
          case Some(dbToken) => displayFiles(user)
        }
      }
    }
  }

  private def displayFiles(user: User): CssSel =
  {
    ".frame *" #> GoogleOperations.getPhotos(user).map(e =>
      "a [href]" #> GoogleOperations.getUri(e) &
        "img [src]" #> e.getMediaThumbnails.get(0).getUrl
    )
  }
}